using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;

namespace PefStudio.Windows
{
    class ExplorerTreeViewWnd : TreeView
    {
        public string FileExtensions
        {
            get
            {
                return m_FileExtensions;
            }
            set
            {
                m_FileExtensions = value.ToLower();
            }
        }
        private string m_FileExtensions = string.Empty;

        protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
        {
            // Remove the placeholder node.
            e.Node.Nodes.Clear();

            // We stored the ShellItem object in the node's Tag property - hah!
            ShellItem shNode = (ShellItem)e.Node.Tag;
            ArrayList arrSub = shNode.GetSubFolders();
            ArrayList arrStorages = shNode.GetSubStorage();
            foreach (ShellItem shStorage in arrStorages)
            {
                if (shStorage.IsFolder == true || shStorage.IsLink == true || shStorage.IsHidden == true)
                    continue;

                //if (string.IsNullOrEmpty(m_FileExtensions) == false && FileExtensions.EndsWith(".*") == false)
                //    if (FileUtility.MatchesPattern(shStorage.DisplayName, FileExtensions) == false)
                //        continue;

                arrSub.Add(shStorage);
            }

            foreach (ShellItem shChild in arrSub)
            {
                if (shChild.IsFileSystem == false)
                    continue;

                TreeNode tvwChild = new TreeNode();
                tvwChild.Text = shChild.DisplayName;
                tvwChild.ImageIndex = shChild.IconIndex;
                tvwChild.SelectedImageIndex = shChild.IconIndex;
                tvwChild.Tag = shChild;

                // If this is a folder item and has children then add a place holder node.
                if (shChild.IsFolder)// && shChild.HasSubFolder)
                    tvwChild.Nodes.Add("PH");
                e.Node.Nodes.Add(tvwChild);
            }

            base.OnBeforeExpand(e);
        }

        PropertyContainer propertyContainer = new PropertyContainer();
        public PropertyContainer PropertyContainer
        {
            get
            {
                return propertyContainer;
            }
        }

        protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
        {
            propertyContainer.SelectedObject = e.Node.Tag;

            base.OnBeforeSelect(e);
        }

        [DllImport("shell32.dll ")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);

        protected override void OnNodeMouseDoubleClick(TreeNodeMouseClickEventArgs e)
        {
            ShellItem shNode = (ShellItem)e.Node.Tag;
            if (shNode.IsFolder == false)
            {
                //foreach (Codon codon in FilterCodons)
                //{
                //    string pattern = codon.Properties.Get("extensions", "");
                //    if (FileUtility.MatchesPattern(shNode.DisplayName, pattern) == true 
                //        && codon.Properties.Contains("class"))
                //    {
                //        AbstractCommand command = codon.AddIn.CreateObject(codon.Properties["class"]) as AbstractCommand;
                //        if(command != null)
                //            command.Run();
                //        return;
                //    }
                //}
                ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(shNode.DisplayName), new StringBuilder(" "), new StringBuilder(shNode.Path), 1); //SW_NORMAL);
            }

            base.OnNodeMouseDoubleClick(e);
        }
    }
}
